/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.folder.stdPatterns.impl;

import java.util.HashSet;

import net.yienyien.process.bpd.BPDComponent;
import net.yienyien.process.bpd.BPDObject;
import net.yienyien.process.bpd.BpdPackage;
import net.yienyien.process.bpd.ControlFlowRelation;
import net.yienyien.process.folder.stdPatterns.StdPatternsPackage;
import net.yienyien.process.folder.stdPatterns.Switch;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Switch</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class SwitchImpl extends StdBPDPatternImpl implements Switch {
	
	public static final String NAME="Switch";
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	protected SwitchImpl() {
		super(NAME, BpdPackage.eINSTANCE.getDataBasedXOR(), BpdPackage.eINSTANCE.getXORMerge(), BpdPackage.eINSTANCE.getTask(), BpdPackage.eINSTANCE.getIntermediateEvent());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return StdPatternsPackage.Literals.SWITCH;
	}

	@Override
	public EList<BPDObject> getContent(BPDObject source, BPDObject sink) {
		HashSet<BPDObject> objs1=new HashSet<BPDObject>();
		EList<BPDObject> set=new BasicEList<BPDObject>();
		
		for(ControlFlowRelation r: source.getOutputs()) {
			objs1.add(r.getOutput());
		}
		for(ControlFlowRelation r: sink.getInputs()) {
			if(objs1.contains(r.getInput())) {
				set.add(r.getInput());
			}
		}

		return set;
	}

	@Override
	public boolean verify(BPDComponent component) {
		top: for(BPDObject o: component.getObjects()) {
			
			if(o.equals(component.getSource()) || o.equals(component.getSink())) {
				continue top;
			}
			
			for(ControlFlowRelation r: o.getInputs()) {
				if(!r.getInput().equals(component.getSource())) {
					return false;
				}
			}
			
			for(ControlFlowRelation r: o.getOutputs()) {
				if(!r.getOutput().equals(component.getSink())) {
					return false;
				}
			}
		}
	
		return true;
	}

} //SwitchImpl
